<?php
require_once "Zen/View/Helper/Image/lib/WideImage.php";
class Zen_View_Helper_Image_Helper extends Zend_View_Helper_Abstract {
	protected $actions = array ();
	protected $cacheId;
	protected $imageId;
	protected $image;
	protected $type;
	protected $url;
	protected $quality = 100;
	
	
	
	public function __call($method, $args) {
		$this->actions [] = array ('method' => $method, 'args' => $args );
		return $this;
	}
	
	/**
	 * @return the $url
	 */
	public function getUrl() {
		$this->execute();
		return $this->url;
	}
	
	
	/**
	 * @return the $type
	 */
	public function getType() {
		return $this->type;
	}
	
	/**
	 * @return the $quality
	 */
	public function getQuality() {
		return $this->quality;
	}
	
	/**
	 * @param field_type $type
	 */
	public function setType($type) {
		$this->type = $type;
	}
	
	/**
	 * @param field_type $quality
	 */
	public function setQuality($quality) {
		$this->quality = $quality;
	}
	 
	protected function execute() {
		$this->quality=100;
		//if(isset($this->type)){
		
		$cacheId = $this->imageId . '_' . md5 (serialize ( $this->actions ).$this->quality.$this->imageId);
		//}else{$cacheId = $this->imageId . '_' . md5 (serialize ( $this->actions ).$this->quality.$this->type);}
		
		$cache_dir = Zend_Registry::get ( 'cnf' )->cache_dir;
		$cache_dir .= "images/";
		
		if (! file_exists ( $cache_dir )) {
			mkdir ( $cache_dir, 777, true );
		}
		
		$cache = Zen_Cache_Factory::get('images');

		
		$image = $cache->load($cacheId);
		if (! $image) {
			$imageModel = new Images_Model_Imageslist();
			$image = $imageModel->getTable()->find($this->imageId)->current ();
			$this->image = $image->toArray();
			if (! $image) {
				return false;
			}
		}else{
			$this->image = $image;
		}
		
		$this->type=$this->image['type'];
		
		$url_dir = str_replace ( ROOT_PATH, '', $cache_dir );
		$image_name = $cache_dir . $cacheId . '.' . $this->getType ();
		$image_url = $url_dir . $cacheId . '.' . $this->getType ();
		
		$this->url = $image_url;
		
	
		
		if (! file_exists ( $image_name )&&(file_exists( ROOT_PATH . $this->image['path']))) {
			if (sizeof ( $this->actions ) > 0) {
				$Wimage = WideImage::load ( ROOT_PATH . $this->image['path'] );
				foreach ( $this->actions as $action ) {
					$make=true;
					
					/*
					 * Условия для $crop
					 */
					if(($action ['method']=='crop')&&(sizeof($action ['args'])==0)){
						if($this->image['crop']!=""){
							$cropParams=Zend_Json_Decoder::decode($this->image['crop']);
							$params=array(
								$cropParams['perc_x']."%",
								$cropParams['perc_y']."%",
								$cropParams['perc_width']."%",
								$cropParams['perc_height']."%",
							);
							$action ['args']=$params;
						}else{
								$make=false;
						}
					}
					/*
					 * Условия для $merge
					 */
					if(($action ['method']=='merge')){
							$action ['args'][0]=WideImage::load($action ['args'][0]);
					}
					
					
					if ($Wimage&&($make==true)) {
						if (method_exists ( $Wimage, $action ['method'] )) {
							$Wimage = call_user_func_array ( array ($Wimage, $action ['method'] ), $action ['args'] );
						}
					}
				}
				$this->image['width'] = $Wimage->getWidth ();
				$this->image['height'] = $Wimage->getHeight ();
				if ($this->type == "png") {
					$Wimage->saveToFile ( $image_name );
				}
				if ($this->type == "jpg" || $this->type == "jpeg") {
					$Wimage->saveToFile ( $image_name, $this->quality );
				}
				if ($this->type == "gif") {
					$Wimage->saveToFile ( $image_name );
				}
				
				$cache->save($this->image,$cacheId,array($this->imageId));		
			}else{
				$this->url = $this->image['img_url'];
			}
		}
	}
	
	public function image_Helper($id) {
		$this->image = false;
		$this->actions = array ();
		$this->cacheId = "";
		$this->quality = 80;
		$this->url = "";
		
		$id = trim ( preg_replace ( '/\D/', '', $id ) );
		if (($id == "") || ($id < 0)) {
			return $this;
		}
		$this->imageId = $id;
		return $this;
	}
	
	public function render($params=false) {
		$url=$this->getUrl ();
		
		if (isset ( $this->image['alt'] )&&(!isset($params['alt']))) {
			$alt = 'alt="' . $this->image['alt'] . '"';
		} else {
			if(!isset($params['alt'])){
				$alt = 'alt=""';
			}
			else{
				$alt = 'alt="' . $params['alt'] . '"';
			}
		}
		if (isset ( $this->image['width'] )) {
			$width = 'width="' . $this->image['width'] . '"';
		} else {
			$width = '';
		}
		if (isset ( $this->image['height'] )) {
			$height = 'height="' . $this->image['height'] . '"';
		} else {
			$height = '';
		}
		
		if (isset ( $params['class'] )) {
			$class = 'class="' . $params['class'] . '"';
		} else {
			$class = '';
		}
		
		
		$img = '<img ' . $class . ' '. $alt . ' ' . $width . ' ' . $height . ' src="' . $url . '"  />';
		$img = preg_replace ( '/\040+/', ' ', $img);
		return $img;
	}

}